package br.com.main.client.system.botoes;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import br.com.main.client.system.botoes.FinanceiroPresenter.Display;

import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.HasKeyboardSelectionPolicy.KeyboardSelectionPolicy;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RadioButton;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.datepicker.client.DateBox;
public class FinanceiroView extends Composite implements Display {


	private HorizontalPanel panelMain;
	private VerticalPanel panelLeft;
	private VerticalPanel panelRight;

	private TextBox tbPesquisar;
	private List<Financas> financeiro;
	private CellTable<Financas> tabela;

	private VerticalPanel panelCadastroMorador;
	private Label lbNovaDespesa;

	private TextBox tbValor;
	private TextBox tbDescricao;
	private HorizontalPanel pnBotoes;
	private HorizontalPanel horitalRadio;
	private Button btCancel;
	private Button btCadastrar;

	private final String radioGrup = "ESCOLHA";
	private RadioButton radioSim;
	private RadioButton radioNao;

	
	DateBox dateBox = new DateBox();

	public static class Financas {
		private String valor;
		private String desrcricao;
		private String data;

		public Financas(String valor, String descricao, String data) {
			this.valor = valor;
			this.desrcricao = descricao;
			this.data = data;
		}
	}

	public FinanceiroView() {
		init();
		initWidget(panelMain);
	}

	private void init() {
		panelMain = new HorizontalPanel();
		panelMain.setSpacing(25);
		panelMain.setBorderWidth(1);

		panelLeft = new VerticalPanel();
		panelRight = new VerticalPanel();
		tbPesquisar = new TextBox();
		financeiro = new ArrayList<Financas>();
		tabela = new CellTable<Financas>();
		tabela.setKeyboardSelectionPolicy(KeyboardSelectionPolicy.ENABLED);

		TextColumn<Financas> valorColumn = new TextColumn<FinanceiroView.Financas>() {

			@Override
			public String getValue(Financas object) {
				// TODO Auto-generated method stub
				return object.valor;
			}
		};
		tabela.addColumn(valorColumn, "Valor");

		TextColumn<Financas> envolvidosColumn = new TextColumn<FinanceiroView.Financas>() {

			@Override
			public String getValue(Financas object) {
				// TODO Auto-generated method stub
				return object.data;
			}
		};
		tabela.addColumn(envolvidosColumn, "Data");

		TextColumn<Financas> descricaoColumn = new TextColumn<FinanceiroView.Financas>() {

			@Override
			public String getValue(Financas object) {
				// TODO Auto-generated method stub
				return object.desrcricao;
			}
		};
		tabela.addColumn(descricaoColumn, "Descrição");
		

		tabela.setRowCount(financeiro.size(), true);
		tabela.setRowData(0, financeiro);

		tbValor = new TextBox();
		tbDescricao = new TextBox();
		btCancel = new Button("Cancelar");
		btCadastrar = new Button("Cadastrar");
		pnBotoes = new HorizontalPanel();
		pnBotoes.setSpacing(5);
		pnBotoes.add(btCancel);
		pnBotoes.add(btCadastrar);


		radioSim = new RadioButton(radioGrup, "Sim");
		radioNao = new RadioButton(radioGrup, "Nao");

		horitalRadio = new HorizontalPanel();
		horitalRadio.add(radioSim);
		horitalRadio.add(radioNao);

		panelCadastroMorador = new VerticalPanel();
		panelCadastroMorador.setBorderWidth(1);
		panelCadastroMorador.setSpacing(5);
		panelCadastroMorador.add(lbNovaDespesa = new Label("Cadastro Nova Despesa"));
		panelCadastroMorador.add(new Label("Valor"));
		panelCadastroMorador.add(tbValor);
		panelCadastroMorador.add(new Label("Descrição"));
		panelCadastroMorador.add(tbDescricao);
		panelCadastroMorador.add(new Label("Data"));
		panelCadastroMorador.add(dateBox);
		panelCadastroMorador.add(new Label("Pago"));
		panelCadastroMorador.add(horitalRadio);
		
		lbNovaDespesa.addStyleName("labelPointer");
	
		
		dateBox.setFormat(new DateBox.DefaultFormat(DateTimeFormat.getFormat("d/MM/y")));
		
		panelCadastroMorador.add(pnBotoes);
		panelCadastroMorador.setVisible(true);

		panelLeft.setBorderWidth(1);
		panelLeft.setSpacing(5);
		panelLeft.add(new Label("Pesquisar "));
		panelLeft.add(tbPesquisar);
		panelLeft.add(new Label("Lista dos gastos"));
		panelLeft.add(tabela);

		panelRight.add(panelCadastroMorador);

		panelMain.add(panelLeft);
		panelMain.add(panelRight);
		
		
		
	}

	@Override
	public Button getBtCadastrar() {
		return btCadastrar;
	}

	@Override
	public List<Financas> getList() {
		return financeiro;
	}
	
	
	@Override
	public Label getLbNovaDespesa() {
		return lbNovaDespesa;
	}
	@Override
	public TextBox getTbValor() {
		return tbValor;
	}
	@Override
	public TextBox getTbDescricao() {
		return tbDescricao;
	}
	@Override
	public Button getBtCancel() {
		return btCancel;
	}
	@Override
	public RadioButton getRadioSim() {
		return radioSim;
	}
	@Override
	public RadioButton getRadioNao() {
		return radioNao;
	}
	@Override
	public DateBox getDateBox() {
		return dateBox;
	}

	@Override
	public void atualizaTabela() {
		tabela.setRowCount(financeiro.size(), true);
		tabela.setRowData(0, financeiro);
	}
	@Override
	public void enableOrDesenable(boolean tipo){
		
		  dateBox.setEnabled(tipo);
		  tbValor.setEnabled(tipo);
		  tbDescricao.setEnabled(tipo);
		  radioNao.setEnabled(tipo);
		  radioSim.setEnabled(tipo);
		  radioNao.setChecked(tipo);;
		  radioSim.setChecked(tipo);
		  
	}
	@Override
	public void clean(){
		 
		  tbValor.setValue("");;
		  tbDescricao.setValue("");
		  radioNao.setChecked(false);;
		  radioSim.setChecked(false);
		  radioNao.setEnabled(false);
		  radioSim.setEnabled(false);
		  dateBox.getTextBox().setValue("");
	}
	
	@Override
	public String[] getDados(){
		String[] dados = new String[4];
		
		dados[0] = tbValor.getValue();
		dados[1] = tbDescricao.getValue();
		dados[2] = dateBox.getTextBox().getValue();
		
		if(radioSim.getValue()){
			dados[3] = "Sim";
		}
		else if(radioNao.getValue()){
			dados[3] = "Não";
		}
		else{
			dados[3] = "Err";
		}
		
		return dados;
		
	}

}
